
"""
open 打开一个文件
    r 只读
    w 只写， 文件不存在将创建， 文件存在将清空源文件
    a 追加
    r+ 读写

with 在不需要访问文件后将其关闭

rstrip() 删除多余的空格和换行
"""

PATH_FILE='/home/dan/Desktop/python_code/learning/011_file1.txt'

def read_test():
    with open(PATH_FILE, 'r', encoding='UTF-8') as f:
        buf = f.read()
        content2 = buf.rstrip()
        print(f"buf='{buf}'")
        print(f"rstrip='{content2}'")

    with open(PATH_FILE, 'r', encoding='UTF-8') as f:
        line_buf = f.readlines()    # 将文件内容保存起来

    ls = ''
    for l in line_buf:
        ls += l.strip()

    print(f"ls='{ls}'")

def write_test():
    with open(PATH_FILE, 'w', encoding='utf-8') as f:
        f.write('qwertyuiop\n')
        f.write('asdfghjkl\n')
        f.write('zxcvbnm\n')

def append_test():
    with open(PATH_FILE, 'a', encoding='utf-8') as f:
        f.write('append txt\n')

# read_test()
# write_test()
append_test()